import { notEmpty } from '~/utils/common.js'

export function setHttp(ComputedSettings, http) {
  if (http.baseUrl && http.baseUrl.length > 0) {
    ComputedSettings.http.baseUrl = http.baseUrl
  }
  if (http.timeoutInMs) {
    ComputedSettings.http.timeoutInMs = http.timeoutInMs
  }
  if (http.mockAll) {
    ComputedSettings.http.mockAll = http.mockAll
  }
  if (http.mockOnDemand) {
    ComputedSettings.http.mockOnDemand = http.mockOnDemand
  }
}

export function setReservedApi(ComputedSettings, api) {
  for (const key in api) {
    if (notEmpty(api[key])) {
      if (ComputedSettings.http.api.reserved[key] == null) {
        ComputedSettings.http.api.reserved[key] = {}
      }
      if (api[key].desc && api[key].desc.length > 0) {
        ComputedSettings.http.api.reserved[key].desc = api[key].desc
      }
      if (api[key].url && api[key].url.length > 0) {
        ComputedSettings.http.api.reserved[key].url = api[key].url
      }
      if (api[key].method && api[key].method.length > 0) {
        ComputedSettings.http.api.reserved[key].method = api[key].method
      }
      if (api[key].mock) {
        ComputedSettings.http.api.reserved[key].mock = api[key].mock
      }
    }
  }
}

export function setCustomApi(ComputedSettings, customApi) {
  for (const module in customApi) {
    if (notEmpty(customApi[module])) {
      const thisModule = customApi[module]
      if (ComputedSettings.http.api.custom[thisModule] == null) {
        ComputedSettings.http.api.custom[thisModule] = {}
      }

      for (const key in thisModule) {
        if (notEmpty(thisModule[key])) {
          if (ComputedSettings.http.api.custom[thisModule][key] == null) {
            ComputedSettings.http.api.custom[thisModule][key] = {}
          }

          if (thisModule[key].desc && thisModule[key].desc.length > 0) {
            ComputedSettings.http.api.custom[thisModule][key].desc = thisModule[key].desc
          }
          if (thisModule[key].url && thisModule[key].url.length > 0) {
            ComputedSettings.http.api.custom[thisModule][key].url = thisModule[key].url
          }
          if (thisModule[key].method && thisModule[key].method.length > 0) {
            ComputedSettings.http.api.custom[thisModule][key].method = thisModule[key].method
          }
          if (thisModule[key].mock) {
            ComputedSettings.http.api.custom[thisModule][key].mock = thisModule[key].mock
          }
        }
      }
    }
  }
}
